﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;

using ChinaSoft.Common.Flexigrid;
using ChinaSoft.DBUtility;

namespace ChinaSoft.DAL.Common
{
    public class FlexigridHelper
    {
        public static JsonFlexiGridData QueryDataForFlexGridByPager(PageView view)
        {
            string order = string.Empty;
            string condtion = string.Empty;//查询条件
            if (view.OrderBy != null && view.OrderBy.Count > 0)
            {
                order = view.OrderBy.ToString();
            }

            if (view.QuickLuanch != null)
            {
                condtion = view.QuickLuanch.ToString();
            }
            string pk = view.PK;

            var Para = new OracleParameter[8];
            Para[0] = new OracleParameter("@TABLENAME_IN", OracleType.NVarChar, 2000) { Value = view.TableName };
            Para[1] = new OracleParameter("@PAGESIZE_IN", OracleType.Int32, 4) { Value = view.PageSize };
            Para[2] = new OracleParameter("@PAGEINDEX_IN", OracleType.Int32, 4) { Value = view.PageIndex };
            Para[3] = new OracleParameter("@ORDERSYNTAX_IN", OracleType.VarChar, 1000) { Value = order };
            Para[4] = new OracleParameter("@CONDITION_IN", OracleType.VarChar, 4000) { Value = condtion };
            Para[5] = new OracleParameter("@FIELDSNAME_IN", OracleType.VarChar, 2000) { Value = view.Fields };
            Para[6] = new OracleParameter("@ONLYFIELD_IN", OracleType.VarChar, 200) { Value = pk };
            Para[7] = new OracleParameter("@COUNT_OUT", OracleType.Int32, 4) { Direction = ParameterDirection.Output };

            JsonFlexiGridData data = new JsonFlexiGridData { page = view.PageIndex + 1, total = view.RecordCount };
            try
            {
                using (OracleDataReader reader = DbHelperOra.RunProcedure("COMMON_PAGING", Para))
                {
                    if (reader.HasRows)
                    {
                        if (data.rows == null)
                        {
                            data.rows = new List<FlexiGridRow>();
                        }
                        while (reader.Read())
                        {
                            FlexiGridRow row = new FlexiGridRow { cell = new List<string>() };
                            for (int i = 0, l = reader.FieldCount; i < l; i++)
                            {
                                string v = reader.IsDBNull(i) ? "" : reader.GetValue(i).ToString();
                                if (pk == reader.GetName(i))
                                {
                                    row.id = v;
                                }
                                row.cell.Add(v);
                            }
                            data.rows.Add(row);
                        }
                    }

                }
                data.total = view.RecordCount = Para[7].Value != null && Para[7].Value != DBNull.Value ? Convert.ToInt32(Para[7].Value) : -1;
                return data;
            }
            catch (DataException ex)
            {
                data.error.ErrorCode = "301";
                data.error.ErrorMessage = ex.Message;
                return data;
            }
        }
    }
}
